<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar title="登录" class="van-nav-bar">
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- 表单 -->
    <van-form @submit="onSubmit" ref="loginFromRef">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入用户名"
        :rules="loginFromRules.mobile"
        maxlength="11"
        type="number"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="loginFromRules.code"
        maxlength="6"
        type="number"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down
            v-if="isCountDownShow"
            :time="1000 * 5"
            format="ss s"
            @finish="isCountDownShow = false"
          />
          <van-button
            v-else
            round
            size="small"
            type="primary"
            class="auth-code-but"
            native-type="button"
            @click="onSmsSend"
          >
            发送验证码
          </van-button>
        </template>
      </van-field>
      <div class="login-but-wrap">
        <van-button block type="info" native-type="submit" class="login-but"
          >提交</van-button
        >
      </div>
    </van-form>
    <!-- 、表单 -->
  </div>
</template>
<script>
import { login, sendSms } from '@/api/user'
export default {
  name: 'loginindex',
  data() {
    return {
      user: {
        mobile: '13886883211',
        code: '246810'
      },
      loginFromRules: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空！'
          },
          {
            patterm: /^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$/,
            message: '请填写正确的手机格式！'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空！'
          },
          {
            patterm: /\d{6}$/,
            message: '请填写正确的验证码！'
          }
        ]
      },
      isCountDownShow: false
    }
  },
  methods: {
    async onSubmit() {
      // 1.获取表单的数据
      const user = this.user
      // 2.验证表单
      // 3.提交表单请求登录
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true,
        duration: 0
      })
      try {
        const { data } = await login(user)
        // 存储数据到vuex与本地存储中
        this.$store.commit('setUser', data.data)
        this.$toast.success('登录成功')
        // 跳转到个人中心
        this.$router.back('/my')
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('登录失败，手机号或验证码错误')
        }
      }
      // 4.token操作
    },
    async onSmsSend() {
      console.log('onSmsSend')
      try {
        await this.$refs.loginFromRef.validate('mobile')
      } catch (err) {
        return false
      }
      this.isCountDownShow = true
      try {
        await sendSms(this.user.mobile)
        this.$toast.success('登录成功')
      } catch (err) {
        this.isCountDownShow = false
        if (err.response.status === 429) {
          this.$toast.fail('发送太频繁，稍后再试')
        } else {
          this.$toast('发送失败，稍后再试')
        }
      }
    }
  }
}
</script>
<style lang="less" scoped>
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .auth-code-but {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    padding: 0;
    font-size: 22px;
    color: #000;
  }
  .login-but-wrap {
    margin: 52px 33px;
  }
  .login-but {
    background-color: violet;
    border: none;
  }
}
</style>
